package com.aipanda.entity;

import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

/**
 * 管理员类
 * 
 * @param username 			用户名
 * @param password 			密码
 * @param email				E-mail
 * @param name 				姓名
 * @param department 		部门
 * @param isEnabled 		是否启用
 * @param isLocked 			是否锁定账号
 * @param loginFailureCount 连续登录失败次数
 * @param lockedDate 		锁定日期
 * @param loginDate 		最后登录日期
 * @param loginIp 			最后登入IP
 * @param roles 			角色
 * @param orders 			订单
 * @author wanyuxiang
 * 
 */
@Entity
@Table(name = "aipanda_admin")
public class Admin extends BaseEntity {
	private static final long serialVersionUID = -7519486823153844426L;
	private String username;
	private String password;
	private String email;
	private String name;
	private String department;
	private Boolean isEnabled;
	private Boolean isLocked;
	private Integer loginFailureCount;
	private Date lockedDate;
	private Date loginDate;
	private String loginIp;
	private Set<Role> roles = new HashSet<Role>();
	private Set<Order> orders = new HashSet<Order>();

	@NotEmpty(groups = { BaseEntity.Save.class })
	@Pattern(regexp = "^[0-9a-z_A-Z\\u4e00-\\u9fa5]+$")
	@Length(min = 2, max = 255)
	@Column(nullable = false, updatable = false, unique = true)
	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@NotEmpty(groups = { BaseEntity.Save.class })
	@Pattern(regexp = "^[^\\s&\"<>]+$")
	@Length(min = 4, max = 255)
	@Column(nullable = false)
	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@NotEmpty
	@Email
	@Length(max = 255)
	@Column(nullable = false)
	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Length(max = 255)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Length(max = 255)
	public String getDepartment() {
		return this.department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	@NotNull
	@Column(name="is_enabled",nullable = false)
	public Boolean getIsEnabled() {
		return this.isEnabled;
	}

	public void setIsEnabled(Boolean isEnabled) {
		this.isEnabled = isEnabled;
	}

	@Column(name="is_locked",nullable = false)
	public Boolean getIsLocked() {
		return this.isLocked;
	}

	public void setIsLocked(Boolean isLocked) {
		this.isLocked = isLocked;
	}

	@Column(name="login_failure_count",nullable = false)
	public Integer getLoginFailureCount() {
		return this.loginFailureCount;
	}

	public void setLoginFailureCount(Integer loginFailureCount) {
		this.loginFailureCount = loginFailureCount;
	}
	
	@Column(name="locked_date")
	public Date getLockedDate() {
		return this.lockedDate;
	}

	public void setLockedDate(Date lockedDate) {
		this.lockedDate = lockedDate;
	}
	@Column(name="login_date")
	public Date getLoginDate() {
		return this.loginDate;
	}

	public void setLoginDate(Date loginDate) {
		this.loginDate = loginDate;
	}
	@Column(name="login_ip")
	public String getLoginIp() {
		return this.loginIp;
	}

	public void setLoginIp(String loginIp) {
		this.loginIp = loginIp;
	}

	@NotEmpty
	@ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
	@JoinTable(name = "aipanda_admin_role",joinColumns ={@JoinColumn(name="admins",nullable=false)},inverseJoinColumns = {@JoinColumn(name="roles",nullable=false)})
	public Set<Role> getRoles() {
		return this.roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	@OneToMany(mappedBy = "operator", fetch = FetchType.LAZY)
	public Set<Order> getOrders() {
		return this.orders;
	}

	public void setOrders(Set<Order> orders) {
		this.orders = orders;
	}

	public Admin preRemove(Admin admin) {
		Set<Order> set = admin.getOrders();
		if (set != null) {
			Iterator<Order> iteratorOrder = set.iterator();
			while (iteratorOrder.hasNext()) {
				Order order = iteratorOrder.next();
				order.setLockExpire(null);
				order.setOperator(null);
			}
		}
		return admin;
	}
}
